﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace PhotoFileMigrator
{
    public class Common
    {
        /// <summary>
        /// 生成文件目录
        /// </summary>
        /// <param name="path">文件或文件目录</param>
        public static void CreatePath(string path)
        {
            path = Path.GetDirectoryName(path);
            if (!System.IO.Directory.Exists(path))
            {
                System.IO.Directory.CreateDirectory(path);
            }
        }
        /// <summary>
        /// 检查文件是否存在
        /// </summary>
        /// <param name="file">目标文件路径</param>
        /// <returns></returns>
        public static bool CheckFile(string file)
        {
            if (System.IO.File.Exists(file))
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 迁移文件文件
        /// </summary>
        /// <param name="sourceFileName">源文件路径</param>
        /// <param name="destFileName">目标文件路径</param>
        /// <param name="IsCut">迁移方式：true,剪切；false,复制</param>
        /// <returns>执行结果</returns>
        public static RetModel MoveOrCopyFile(string sourceFileName, string destFileName, bool IsCut)
        {
            RetModel ret = new RetModel();
            try
            {
                if (!CheckFile(sourceFileName))
                {
                    ret.RetrunState = ReturnType.FileNotFound;
                    ret.ReturmMsg = string.Format("找不到源文件:{0}", sourceFileName);
                    return ret;
                }
                else
                {
                    CreatePath(destFileName);
                    if (IsCut)
                    {
                        if (CheckFile(destFileName))
                        {
                            //ret.RetrunState = ReturnType.FileExists;
                            //ret.ReturmMsg = string.Format("迁移文件已存在:{0}", destFileName);
                            //return ret;
                            System.IO.File.Copy(sourceFileName, destFileName, true);//复制
                        }
                        System.IO.File.Move(sourceFileName, destFileName);//剪切
                    }
                    else
                    {
                        System.IO.File.Copy(sourceFileName, destFileName, true);//复制
                    }
                }

                if (CheckFile(destFileName))
                {
                    ret.RetrunState = ReturnType.FileNotFound;
                    ret.ReturmMsg = string.Format("源文件迁移失败，源路径：{0}，目标路径{1}，迁移方式：{2}", sourceFileName, destFileName, IsCut ? "剪切" : "复制");
                }
            }
            catch (Exception ex)
            {
                ret.RetrunState = ReturnType.UnKnown;
                ret.ReturmMsg = ex.ToString();
            }
            return ret;
        }


        public static List<string> ToList(string dataRow)
        {
            if (string.IsNullOrEmpty(dataRow))
            {
                return new List<string>();
            }
            List<string> list = new List<string>(dataRow.Split('\t'));
            return list;
        }

        /// <summary>
        /// 保存数据到CSV文件
        /// </summary>
        /// <param name="dataRow">数据</param>
        /// <param name="csvFile">CSV文件路径</param>
        /// <returns>是否成功</returns>
        public static bool SaveDataToCSV(string dataRow, string csvFile)
        {
            if (!CheckFile(csvFile))
            {
                CreatePath(csvFile);
            }
            System.IO.File.AppendAllText(csvFile, dataRow + "\r\n");
            return true;
        }
    }
}
